#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui -> setupUi(this);

    configInit();
    uiInit();
    connectInit();
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::setBackgroung(QColor color) {
    pale = palette();
    pale.setColor(QPalette::Window, color);
    setPalette(pale);
}

void MainWindow::setUrl(QString url) {
    ui -> labFileUrl -> setText(url);
    ui -> labFileUrl -> setToolTip(tr(url.toLocal8Bit().data()));
    args[1] = url;
}

void MainWindow::configInit() {
    deal_process = new DealProcess; //初始化命令处理类
    command = "CertUtil";
    args << "-hashfile" << "" << "md5";
    file_dialog = new QFileDialog;
    color.setRgb(243, 243, 243, 255);       //窗口背景色
    drop_color.setRgb(230, 230, 230, 250);  //拖拽进入后的窗口背景色
    clipboard = QApplication::clipboard();

    setWindowTitle("文件校验码生成器");
    setAcceptDrops(true);   //允许放下操作
    setBackgroung(color);
}

void MainWindow::uiInit() {
    //QGroupBox init.
    ui -> grbChooseFile -> setTitle("选择校验文件");
    ui -> grbResult -> setTitle("运行结果");
    ui -> grbValue -> setTitle("校验值");

    //QPushButton init.
    ui -> btnChooseFile -> setText("选择文件");
    ui -> btnCreateFileChecker -> setText("获取校验");
    ui -> btnCopyCode -> setText("复制代码");

    //QComboBox init
    QStringList str_list_check_type;
    str_list_check_type << "MD2" << "MD4" << "MD5" << "SHA1" << "SHA256" << "SHA384" << "SHA512";
    for(int i = 0; i < str_list_check_type.length(); i ++) {
        ui -> cbbCheckType -> addItem(str_list_check_type[i], str_list_check_type[i]);
    }
    ui -> cbbCheckType -> setCurrentText("MD5");

    //QPlainTextEdit
    ui -> ptedResult -> setDisabled(true);

    //QMessageBox
    message_box = new QMessageBox;
    message_box -> setButtonText(QMessageBox::Close, QString("关闭"));

    //QLabel
    ui -> labFileUrl -> setText("");
}

void MainWindow::connectInit() {
    //QPushButton
    connect(ui -> btnChooseFile, &QPushButton::clicked, [=] {
        QString url;
        url = file_dialog -> getOpenFileName(this,
                                             tr("选择文件"),
                                             QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
                                             tr("所有文件(*.*);;压缩文件(*.zip *.rar *.7z);;"));
        setUrl(url);
    });

    connect(ui -> btnCreateFileChecker, &QPushButton::clicked, [=] {
        QString res = "";

        if(args[1] != "") {
            res = QString::fromLocal8Bit(deal_process -> exec(command, args));
            ui -> ptedResult -> clear();
            ui -> ptedResult -> appendPlainText(res);
            ui -> ledCheckerCode -> setText(res.split("\r\n")[1].split(' ').join(""));
        }
        else {
            message_box -> critical(this, "温馨提示", "请选择即将校验的文件！", QMessageBox::Close);
        }
    });

    connect(ui -> btnCopyCode, &QPushButton::clicked, [=] {
        QString checker_code = ui -> ledCheckerCode -> text();  //获取校验码

        if(checker_code != "") {    // 判断校验码是否为空
            clipboard -> setText(checker_code);
        }
    });

    //QComboBox
    connect(ui -> cbbCheckType, &QComboBox::currentTextChanged, [=](QString check_type) {
        args[2] = check_type;
    });
}

void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
    e -> acceptProposedAction();    //接收放下操作

    setBackgroung(drop_color);
}

void MainWindow::dragLeaveEvent(QDragLeaveEvent *e) {
    Q_UNUSED(e);

    setBackgroung(color);
}

void MainWindow::dropEvent(QDropEvent *e) {
    setBackgroung(color);

    QList<QUrl> urls = e -> mimeData() -> urls();

    if(urls.length() != 1) {
        message_box -> critical(this, "友情提示", "不接受多个文件，请拖拽单个\r\n文件！", QMessageBox::Close);
        return;
    }

    QString url = urls[0].path().mid(1);

    if(QFileInfo(url).isDir()) {
        message_box -> critical(this, "友情提示", "不接受文件夹，请拖拽单个\r\n文件！", QMessageBox::Close);
        return;
    }

    setUrl(url);
}
